/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sv.com.intesal.compra.controlador;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import sv.com.intesal.clases.abstracts.ControladorAbstract;
import sv.com.intesal.compra.bean.Linea;
import sv.com.intesal.compra.sql.SQLLinea;

/**
 *
 * @author ejerez
 */
public class LineaControlador extends ControladorAbstract {

    public void insert(Linea l) throws Exception {
        try {
            super.setPreparedStatement(SQLLinea.insert);
            super.ps.setString(1, l.getNombre_l());
            super.ps.setString(2, super.v.getEstado(l.getEstado_filtro_l()));
            super.ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public void update(Linea l) throws Exception {
        try {
            super.setPreparedStatement(SQLLinea.update);
            super.ps.setString(1, l.getNombre_l());
            super.ps.setString(2, super.v.getEstado(l.getEstado_filtro_l()));
            super.ps.setInt(3, l.getCodigo_l());
            super.ps.executeUpdate();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public List<Linea> getLineas() throws Exception {
        List<Linea> lista = new ArrayList<Linea>();
        try {
            this.getExecuteQuery(SQLLinea.getLineas);
            while (super.rs.next()) {
                lista.add(this.getLinea(super.rs));
            }
            return lista;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    public List<Linea> getLineasActivos() throws Exception {
        List<Linea> lista = new ArrayList<Linea>();
        try {
            this.getExecuteQuery(SQLLinea.getLineasActivos);
            while (super.rs.next()) {
                lista.add(this.getLinea(super.rs));
            }
            return lista;
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            super.setCloseConexion();
        }
    }

    private Linea getLinea(ResultSet rs) throws Exception {
        try {
            Linea l = new Linea();
            l.setCodigo_l(rs.getInt("codigo_l"));
            l.setNombre_l(rs.getString("nombre_l"));
            l.setEstado_l(rs.getString("estado_l"));
            l.setEstado_filtro_l(v.getEstado(rs.getString("estado_l")));
            return l;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }
}
